home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / alib12 / intface.c < prev    next >
Text File  |  1992-03-09  |  8KB  |  292 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /*    Copyright 1991 by Wayne E. McDaniel                      */
  4. /*    All Rights Reserved                                      */
  5. /*    AutoLibrary is a Trademark of Avid Software              */
  6. /*                                                             */
  7. /***************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include "al.h"
  12.  
  13. /* Include file only required for AutoLibrary system source files. */
  14. #include "al_sys.h"
  15.  
  16.  
  17. #include "\gls\gf.h"
  18. #include "\gls\asiports.h"
  19.  
  20. /********************************************************/
  21.     int         /* The return status. */
  22. CommInterfaceClose (com, info, opts)
  23.     int com;    /* The AutoLibrary communication port. */
  24.     int *info;
  25.     char *opts; /* Options. */
  26. /********************************************************/
  27. {
  28. int err;
  29.  
  30.     opts = opts; /* for compiler */
  31.  
  32.     /* Start out with no errors. */
  33.     *info = ERR_OK;
  34.  
  35.     err = asiquit (comlist[com]->hw_port);
  36.     if (err != ASSUCCESS) {
  37.     *info = err;
  38.     return(ERR_COMM_PACKAGE);
  39.     }
  40.  
  41.     return(ERR_OK);
  42. }
  43.  
  44. /********************************************************/
  45.     int
  46. CommInterfaceOpen (com, device, info, opts)
  47.     int com;      /* The AutoLibrary communication port. */
  48.     char *device; /* The device name. */
  49.     int *info;
  50.     char *opts;   /* Options. */
  51. /********************************************************/
  52. {
  53. int mode, word_len, baud, stop_bits, parity, port_num, strip, dtr, rts, xon;
  54. unsigned int rx_length, tx_length;
  55. char sc, control;
  56. char temp_buf[85];
  57. int s_num, err;
  58. long temp_long;
  59.  
  60.     /* Start out with no errors. */
  61.     *info = ERR_OK;
  62.  
  63.     /* Let valid input for device be com1 or 1 (for example) */
  64.     if (sscanf (device, "com%d", &port_num) == 0) {
  65.  
  66.     if (sscanf (device, "%d", &port_num) == 0) {
  67.         *info = ASINVPORT;
  68.         return (ERR_COMM_PACKAGE);
  69.     }
  70.     }
  71.  
  72.     /* Make sure wild number did not make it through. */
  73.     if ((port_num < 1) || (port_num > 17)) {
  74.     *info = ASINVPORT;
  75.     return (ERR_COMM_PACKAGE);
  76.     }
  77.  
  78.     /* For Greenleaf COM1 is defined as 0. */
  79.     port_num--;
  80.  
  81.     /* Save the port number for closing. */
  82.     comlist[com]->hw_port = port_num;
  83.  
  84.     /* No ASCII device names in this comm package. */
  85.     comlist[com]->hw_port_name = (char *)NULL;
  86.  
  87.     mode = 0;
  88.     baud = 9600;
  89.     parity = P_ODD;
  90.     stop_bits = 1;
  91.     word_len = 8;
  92.     dtr = 0;
  93.     rts = 0;
  94.     rx_length = 8192;
  95.     tx_length = 512;
  96.     strip = ASCII;
  97.     xon = TRUE;
  98.  
  99.     s_num = 0; /* Required for AvidProcessOptions. */
  100.     while ((sc = AvidProcessOptions (AVID, opts, "", &control,
  101.                            temp_buf, &s_num)) != '\0') {
  102.  
  103.     if ((o_strip_yes[0] == control) && (o_strip_yes[1] == sc))
  104.         strip = ASCII;
  105.  
  106.     if ((o_strip_no[0] == control) && (o_strip_no[1] == sc))
  107.         strip = BINARY;
  108.  
  109.     if ((o_even[0] == control) && (o_even[1] == sc))
  110.         parity = P_EVEN;
  111.  
  112.     if ((o_odd[0] == control) && (o_odd[1] == sc))
  113.         parity = P_ODD;
  114.  
  115.     if ((o_none[0] == control) && (o_none[1] == sc))
  116.         parity = P_NONE;
  117.  
  118.     if ((o_baud[0] == control) && (o_baud[1] == sc))
  119.         baud = atoi (temp_buf);
  120.  
  121.     if ((o_stop_bits_1[0] == control) && (o_stop_bits_2[1] == sc))
  122.         stop_bits = 1;
  123.  
  124.     if ((o_stop_bits_2[0] == control) && (o_stop_bits_2[1] == sc))
  125.         stop_bits = 2;
  126.  
  127.     if ((o_word_len_7[0] == control) && (o_word_len_7[1] == sc))
  128.         word_len = 7;
  129.  
  130.     if ((o_word_len_8[0] == control) && (o_word_len_8[1] == sc))
  131.         word_len = 8;
  132.  
  133.     if ((o_dtr[0] == control) && (o_dtr[1] == sc))
  134.         dtr = 1;
  135.  
  136.     if ((o_xon_yes[0] == control) && (o_xon_yes[1] == sc))
  137.         xon = TRUE;
  138.  
  139.     if ((o_xon_no[0] == control) && (o_xon_no[1] == sc))
  140.         xon = FALSE;
  141.  
  142.     if ((o_rx_len[0] == control) && (o_rx_len[1] == sc)) {
  143.         temp_long = atol (temp_buf);
  144.         rx_length = temp_long;
  145.     }
  146.  
  147.     if ((o_tx_len[0] == control) && (o_tx_len[1] == sc)) {
  148.         temp_long = atol (temp_buf);
  149.         tx_length = temp_long;
  150.     }
  151.     }
  152.  
  153.     mode = strip | ASINOUT | NORMALRX;
  154.  
  155.     err = asiopen (comlist[com]->hw_port, mode, rx_length, tx_length,
  156.             baud, parity, stop_bits, word_len, dtr,    rts);
  157.     if (err != ASSUCCESS) {
  158.     *info = err;
  159.     return(ERR_COMM_PACKAGE);
  160.     }
  161.  
  162.     if (xon) {
  163.     err = asixon(comlist[com]->hw_port, 30, 70, XON, XOFF);
  164.     if (err != ASSUCCESS) {
  165.         *info = err;
  166.         return(ERR_COMM_PACKAGE);
  167.     }
  168.     }
  169.  
  170.     return(ERR_OK);
  171. }
  172.  
  173. /********************************************************/
  174.     int
  175. CommInterfaceRead (com, r_timeout, e_timeout, info, opts)
  176.     int com;         /* The AutoLibrary communication port. */
  177.     long r_timeout;  /* The repeating timeout. */
  178.     long e_timeout;  /* The ending timeout. */
  179.     int *info;
  180.     char *opts;      /* Options. */
  181. /********************************************************/
  182. {
  183. int c;
  184. long seconds;
  185.  
  186.     /* Start out with no errors. */
  187.     *info = ERR_OK;
  188.  
  189.     /* Have to check for an ending timeout. */
  190.     time (&seconds);
  191.     if (seconds >= e_timeout) {
  192.     *info = ERR_ENDING_TIMEOUT;
  193.     return(ERR_EOF);
  194.     }
  195.  
  196.     /* Try to get a character without setting up for a timeout. */
  197.     c = asigetc (comlist[com]->hw_port);
  198.     if ((c >=0) && (c <= 0xFF)) {
  199.     EveryChar(c);
  200.     return(c);
  201.     }
  202.  
  203.     if (c == ASINVPORT) {
  204.     *info = ASINVPORT;
  205.     return(ERR_COMM_PACKAGE);
  206.     }
  207.  
  208.     if (c == ASNOTSETUP) {
  209.     *info = ASNOTSETUP;
  210.     return(ERR_COMM_PACKAGE);
  211.     }
  212.  
  213.     /* A timeout is required so set up for it. */
  214.     time(&seconds);
  215.     if (r_timeout < seconds)
  216.     r_timeout = r_timeout + seconds;
  217.  
  218.     while (1 > 0) {
  219.  
  220.     /* Do not try to get a character if an ending timeout has occurred. */
  221.     time (&seconds);
  222.     if (seconds >= e_timeout) {
  223.         *info = ERR_ENDING_TIMEOUT;
  224.         return(ERR_EOF);
  225.     }
  226.  
  227.     /* Try to get a character. */
  228.     c = asigetc (comlist[com]->hw_port);
  229.     if ((c >=0) && (c <= 0xFF)) {
  230.         EveryChar(c);
  231.         return(c);
  232.     }
  233.  
  234.     if (c == ASBUFREMPTY) {
  235.  
  236.         time (&seconds);
  237.         if (seconds >= r_timeout) {
  238.         *info = ERR_REPEAT_TIMEOUT;
  239.         return(ERR_EOF);
  240.         }
  241.  
  242.     } else {
  243.         /* Error ASINVPORT or ASNOTSETUP occurred. */
  244.         *info = c;
  245.         return(ERR_COMM_PACKAGE);
  246.     }
  247.     }
  248.  
  249.     opts = opts; /* for compiler */
  250.  
  251.     /* Nothing should reach here but if it does... */
  252.     *info = c;
  253.     return(ERR_COMM_PACKAGE);
  254. }
  255.  
  256. /********************************************************/
  257.     int
  258. CommInterfaceSend (com, bef, bet, s, len, info, opts)
  259.     int com; /* The AutoLibrary communication port. */
  260.     int bef; /* The number of milliseconds to delay before sending first character. */
  261.     int bet; /* The number of milliseconds to delay between each character. */
  262.     char *s; /* The string to send. */
  263.     int len; /* The length of the string. May not equal strlen(s) because of null characters in the string. */
  264.     int *info;
  265.     char *opts; /* Options. */
  266. /********************************************************/
  267. {
  268. int i, status;
  269.  
  270.     opts = opts; /* for compiler */
  271.  
  272.     /* Start out with no errors. */
  273.     *info = ERR_OK;
  274.  
  275.     /* The delay before the first character sent. */
  276.     delay (bef);
  277.  
  278.     for (i=0; i<len; i++) {
  279.  
  280.     /* The delay before the first character sent. */
  281.     delay (bet);
  282.  
  283.     status = asiputc (comlist[com]->hw_port, s[i]);
  284.     if (status != ASSUCCESS) {
  285.         *info = status;
  286.         return(ERR_COMM_PACKAGE);
  287.     }
  288.     }
  289.  
  290.     return(ERR_OK);
  291. }
  292.